﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Classes;

namespace OfficeManager_v0._1
{
    public partial class AddOrEditOffice : Form
    {
        Organization mOrganization;
        bool edit;
        Office oldOffice;

        public AddOrEditOffice(Organization aOrganization, bool edit, Office aOffice = null)
        {
            mOrganization = aOrganization;
            this.edit = edit;
            InitializeComponent();

            comboBoxDepartment.DropDownStyle = ComboBoxStyle.DropDownList;

            if (edit)
            {
                oldOffice = aOffice;
                textBoxArea.Text = oldOffice.Area.AreaInQuadratMeter.ToString();
                textBoxName.Text = oldOffice.Name;
                
                //department code here
            }
            PopulateDepartmentMenu();
        }

        private void PopulateDepartmentMenu()
        {
            comboBoxDepartment.Enabled = (mOrganization.Departments.Count != 0);
            foreach (Department department in mOrganization.Departments)
                comboBoxDepartment.Items.Add(department.Name + " ID:" + department.ID);

            if (edit && oldOffice.HostDepartment != null)
                comboBoxDepartment.SelectedItem = oldOffice.HostDepartment.Name + " ID:" + oldOffice.HostDepartment.ID;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                Department target = null;
                if (comboBoxDepartment.SelectedItem != null)
                    target = mOrganization.Departments.Find((dep) =>
                    {
                        return dep.Name + " ID:" + dep.ID == comboBoxDepartment.GetItemText(comboBoxDepartment.SelectedItem);
                    });

                if (target == null && comboBoxDepartment.GetItemText(comboBoxDepartment.SelectedItem) != "")
                    throw new Exception();

                if (!edit)
                {
                    Office office = new Office();
                    office.TelephonePost = new List<TelephonePost>();
                    office.Area = new Area();
                    office.Area.AreaInQuadratMeter = int.Parse(textBoxArea.Text);
                    office.HostDepartment = target;
                    office.Name = textBoxName.Text;
                    if (target != null)
                        target.Offices.Add(office);

                    mOrganization.Offices.Add(office);
                }
                else
                {
                    oldOffice.Area.AreaInQuadratMeter = int.Parse(textBoxArea.Text);
                    if (oldOffice.HostDepartment != null)
                        oldOffice.HostDepartment.Offices.Remove(oldOffice);
                    oldOffice.HostDepartment = target;
                    oldOffice.Name = textBoxName.Text;
                    target.Offices.Add(oldOffice);
                }
                
            }
            catch (Exception exc)
            {
                MessageBox.Show("Въведените данни са некоректни!");
                return;
            }

            this.Close();
            this.Dispose();
        }
    }
}
